package com.lex.cheeseStrip.util;

import com.lex.cheeseStrip.app.WebSocketCallback;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_17;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.net.URISyntaxException;

/**
 * Created by Mc on 2016/10/17.
 */

public class WebSocketUtil extends WebSocketClient {

    private static String URL = "ws://43.254.53.215:80/jyguanjia/websocket/1";
//    private static String URL = "ws://139.196.216.185:8080/jyguanjia/websocket/1";
    private static Draft_17 DRAFT = new Draft_17();
    private WebSocketCallback mWebSocketListener;

    public void setWebSocketListener(WebSocketCallback webSocketListener) {
        mWebSocketListener = webSocketListener;
    }

    public WebSocketUtil() throws URISyntaxException {
        super(new URI(URL), DRAFT);
    }

    public WebSocketUtil(WebSocketCallback mWebSocketListener) throws URISyntaxException {
        super(new URI(URL), DRAFT);
        this.mWebSocketListener = mWebSocketListener;

    }

    @Override
    public void onOpen(ServerHandshake handshake) {

        mWebSocketListener.onOpen(handshake);
    }

    @Override
    public void onMessage(String s) {
        mWebSocketListener.onMessage(s);

    }

    @Override
    public void onClose(int i, String s, boolean b) {
        mWebSocketListener.onClose(i,s,b);
    }

    @Override
    public void onError(Exception e) {
        mWebSocketListener.onError(e);
    }

    public interface WebSocketListener {

        void onOpen(ServerHandshake handshake);

        void onMessage(String s);

        void onClose(int i, String s, boolean b);

        void onError(Exception e);

    }


}
